/*  * Author: 凌逆战 | Never
 * * Data: 2023/7/5
 * * Description:
 *
 */
#include <stdio.h>
#include <string.h>
#include "resampler.h"
#include "common.h"
#include "data_utils.h"


int main(int argc, char const* argv[]) {
    FILE* fp_in = fopen("../file/96kHz_dual_input.pcm", "rb");
    FILE* fp_16kHz = fopen("../file/16kHz_dual_output.pcm", "wb");
    FILE* fp_96kHz = fopen("../file/96kHz_dual_output.pcm", "wb");

    int i, r;
    unsigned long read_samples;
    // 96k global temp buffer
    short in_buf_96k_i16[FRAME_LEN_96K * 2] = {0}, out_buf_96k_i16[FRAME_LEN_96K * 2] = {0};

    short in_bufL_96k_i16[FRAME_LEN_96K] = {0}, in_bufR_96k_i16[FRAME_LEN_96K] = {0};
    short out_bufL_96k_i16[FRAME_LEN_96K] = {0}, out_bufR_96k_i16[FRAME_LEN_96K] = {0};
    float in_bufL_96k_f32[FRAME_LEN_96K] = {0.0f}, in_bufR_96k_f32[FRAME_LEN_96K] = {0.0f};
    float out_bufL_96k_f32[FRAME_LEN_96K] = {0.0f}, out_bufR_96k_f32[FRAME_LEN_96K] = {0.0f};
    // 16k global temp buffer
    short in_buf_16k_i16[FRAME_LEN_16K * 2] = {0}, out_buf_16k_i16[FRAME_LEN_16K * 2] = {0};
    short in_bufL_16k_i16[FRAME_LEN_16K] = {0}, in_bufR_16k_i16[FRAME_LEN_16K] = {0};
    short out_bufL_16k_i16[FRAME_LEN_16K] = {0}, out_bufR_16k_i16[FRAME_LEN_16K] = {0};
    float in_bufL_16k_f32[FRAME_LEN_16K] = {0.0f}, in_bufR_16k_f32[FRAME_LEN_16K] = {0.0f};
    float out_bufL_16k_f32[FRAME_LEN_16K] = {0.0f}, out_bufR_16k_f32[FRAME_LEN_16K] = {0.0f};

    static float in_buf_16k_f32[FRAME_LEN_16K * 2] = {0.0f};
    static float out_buf_16k_f32[FRAME_LEN_16K * 2] = {0.0f};

    // resample variable
    float mem_downsamplingL[4 * PSAP_BIQUAD_ORDER] = {0.0f};  // 用于下采样低通滤波器
    float mem_downsamplingR[4 * PSAP_BIQUAD_ORDER] = {0.0f};  // 用于下采样低通滤波器
    float mem_upsamplingL[4 * PSAP_BIQUAD_ORDER] = {0.0f};    // 用于上采样低通滤波器
    float mem_upsamplingR[4 * PSAP_BIQUAD_ORDER] = {0.0f};    // 用于上采样低通滤波器

    static float old_bufL_end = 0.0f, old_bufR_end = 0.0f;

    while ((read_samples = fread(in_buf_96k_i16, sizeof(short), FRAME_LEN_96K*2, fp_in)) != 0) {
        if (read_samples != FRAME_LEN_96K*2) {
            printf("read_samples: %ld\n", read_samples);
            break;
        }

        for (i = 0; i < FRAME_LEN_96K; i++) {
            in_bufL_96k_i16[i] = in_buf_96k_i16[0 + CHANNEL_NUM * i];
            in_bufR_96k_i16[i] = in_buf_96k_i16[1 + CHANNEL_NUM * i];
        }

        convert_i16_to_f32(in_bufL_96k_i16, FRAME_LEN_96K, in_bufL_96k_f32);
        convert_i16_to_f32(in_bufR_96k_i16, FRAME_LEN_96K, in_bufR_96k_f32);

        // Resampler from 96kHz to 16kHz -------------------------------
        Lowpass7k_96k(in_bufL_96k_f32, FRAME_LEN_96K, mem_downsamplingL);
        Lowpass7k_96k(in_bufR_96k_f32, FRAME_LEN_96K, mem_downsamplingR);
        // 输出数据流长度，96k->16k时，缩短1/6
        for (i = 0, r = 0; i < FRAME_LEN_96K - 1; i += 6, r++) {
            in_bufL_16k_f32[r] = in_bufL_96k_f32[i];
            in_bufR_16k_f32[r] = in_bufR_96k_f32[i];
        }
        // 写 16kHz -----------------------------------
        convert_f32_to_i16(in_bufL_16k_f32, FRAME_LEN_16K, out_bufL_16k_i16);
        convert_f32_to_i16(in_bufR_16k_f32, FRAME_LEN_16K, out_bufR_16k_i16);
        for (i = 0; i < FRAME_LEN_16K; i++) {
            out_buf_16k_i16[0 + CHANNEL_NUM * i] = out_bufL_16k_i16[i];
            out_buf_16k_i16[1 + CHANNEL_NUM * i] = out_bufR_16k_i16[i];
        }
        fwrite(&out_buf_16k_i16, sizeof(short), FRAME_LEN_16K*2, fp_16kHz);
        // ------------------------------------------------
        memcpy(in_buf_16k_f32, in_bufL_16k_f32, sizeof(float) * FRAME_LEN_16K);
        memcpy(in_buf_16k_f32 + FRAME_LEN_16K, in_bufR_16k_f32, sizeof(float) * FRAME_LEN_16K);

        memcpy(out_buf_16k_f32, in_buf_16k_f32, sizeof(float) * FRAME_LEN_16K*2);

        memcpy(out_bufL_16k_f32, out_buf_16k_f32, sizeof(float) * FRAME_LEN_16K);
        memcpy(out_bufR_16k_f32, out_buf_16k_f32 + FRAME_LEN_16K, sizeof(float) * FRAME_LEN_16K);

        // 插值16k->96k
        Inter16kto96k(out_bufL_16k_f32, FRAME_LEN_16K, &old_bufL_end, out_bufL_96k_f32);
        Inter16kto96k(out_bufR_16k_f32, FRAME_LEN_16K, &old_bufR_end, out_bufR_96k_f32);
        // 下采样抗周期延拓
        Lowpass7k_96k(out_bufL_96k_f32, FRAME_LEN_96K, mem_upsamplingL);
        Lowpass7k_96k(out_bufR_96k_f32, FRAME_LEN_96K, mem_upsamplingR);
        convert_f32_to_i16(out_bufL_96k_f32, FRAME_LEN_96K, out_bufL_96k_i16);
        convert_f32_to_i16(out_bufR_96k_f32, FRAME_LEN_96K, out_bufR_96k_i16);

        for (i = 0; i < FRAME_LEN_96K; i++) {
            out_buf_96k_i16[0 + CHANNEL_NUM * i] = out_bufL_96k_i16[i];
            out_buf_96k_i16[1 + CHANNEL_NUM * i] = out_bufR_96k_i16[i];
        }
        fwrite(&out_buf_96k_i16, sizeof(short), FRAME_LEN_96K*2, fp_96kHz);
    }
    printf("wav read over\n");
    return 0;
}